package com.ruoyi.kong.fegin;

import com.ruoyi.kong.entity.*;
import com.ruoyi.kong.factory.FeginClientFactory;
import com.ruoyi.kong.proxy.IKongClientProxy;
import feign.Headers;
import feign.Param;
import feign.RequestLine;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

/**
 * kong 服务相关 处理层
 */
public class KongClientService {

    private IKongClientProxy feginClient;

    private KongClientService(String _baseUrl) {

        feginClient = FeginClientFactory.getFeginClient(IKongClientProxy.class, _baseUrl);
    }

    public static KongClientService getInstance(String _baseUrl) {
        return new KongClientService(_baseUrl);
    }

    /**
     * 请求kong 服务器 获取 kong配置信息
     *
     * @return
     */
    public Map kongInfo() {
        Map info = feginClient.info();
        return info;
    }

    /**
     * 请求kong 服务器 获取 kong状态
     *
     * @return
     */
    public Map kongStatus() {
        Map status = feginClient.status();
        return status;
    }

    /**
     * 返回全部 服务信息
     */
    public KongEntity<KongServiceResult> getKongServiceInfos() {
        return feginClient.services();
    }


    /**
     * 根据routeId 获取服务信息
     *
     * @param routeId
     * @return
     */
    public KongService getServiceByRouteId(String routeId) {
        return feginClient.serviceByRouteId(routeId);
    }

    /**
     * 根据serviceId 获取服务信息
     *
     * @param serviceId
     * @return
     */
    public KongService getServiceById(String serviceId) {
        return feginClient.serviceById(serviceId);
    }


    /**
     * 根据插件id 获取服务信息
     *
     * @param pluginId
     * @return
     */
    public KongService getServiceByPluginId(String pluginId) {
        return feginClient.serviceByPluginId(pluginId);
    }

    /**
     * 新增服务
     *
     * @param kongService
     * @return
     */
    public KongEntity<KongService> addService(KongService kongService) {
        return feginClient.addServices(kongService);
    }

    /**
     * 根据id删除服务
     *
     * @param serviceId
     */
    public void deleteServices(String serviceId) {
        feginClient.deleteServices(serviceId);
    }

    /**
     * 修改服务信息
     *
     * @param serviceId
     * @param kongService
     * @return
     */
    public KongEntity<KongService> updateServices(String serviceId, KongService kongService) {
        return feginClient.updateServices(serviceId, kongService);
    }


    /**
     * 返回所有 路由信息
     *
     * @return
     */
    public KongEntity<KongRoute> getRouteList() {
        return feginClient.routes();
    }

    /**
     * 路由新增
     *
     * @param kongRoute
     * @return
     */
    public KongRoute addRoute(KongRoute kongRoute) {
        return feginClient.addRoute(kongRoute);
    }

    /**
     * 删除路由
     *
     * @param routeId
     */
    public void deleteRoute(String routeId) {
        feginClient.deleteRoute(routeId);
    }

    /**
     * @param serviceId
     * @return
     */
    public KongEntity<KongRoute> routeByServiceId(String serviceId) {
        return feginClient.routeByServiceId(serviceId);
    }


    public KongRoute routeById(@Param("routeId") String routeId) {
        return feginClient.routeById(routeId);
    }

    public KongRoute updateRoute(String routeId, KongRouteVo kongRoute) {
        return feginClient.updateRoute(routeId, kongRoute);
    }

    /*****************************插件****************************/
    public KongEntity<KongPlugin> plugins() {
        return feginClient.plugins();
    }


    /**
     * 新增插件
     *
     * @param kongPlugin
     * @return
     */
    public KongEntity<KongPlugin> addPlugin(KongPlugin kongPlugin) {
        return feginClient.addPlugin(kongPlugin);
    }


    public KongPlugin pluginById(String id) {
        return feginClient.pluginById(id);
    }


    public KongEntity<KongPlugin> pluginsByRouteId(String routeId) {
        return feginClient.pluginsByRouteId(routeId);
    }


    public KongEntity<KongPlugin> pluginsByServiceId(String serviceId) {
        return feginClient.pluginsByServiceId(serviceId);
    }

    public Map pluginSchemaByName(String name) {
        return feginClient.pluginSchemaByName(name);
    }

    public void deletePlugin(String id) {
        feginClient.deletePlugin(id);
    }

    public KongPlugin updatePlugin(String id, KongPluginVo plugin) {
        return feginClient.updatePlugin(id, plugin);
    }

    /******************************上游服务**************************/
    /**
     * 返回 上游服务 列表
     *
     * @return
     */
    public KongEntity<KongUpstream> upstreams() {
        return feginClient.upstreams();
    }


    public KongUpstream addUpstream(KongUpstreamVo kongUpstream) {
        return feginClient.addUpstream(kongUpstream);
    }


    public KongUpstream updateUpstream(String upstreamId, KongUpstreamVo kongUpstream) {
        return feginClient.updateUpstream(upstreamId, kongUpstream);
    }


    public void deleteUpstream(String upstreamId) {
        feginClient.deleteUpstream(upstreamId);
    }


    public KongUpstream upstreamsById(String id) {
        return feginClient.upstreamsById(id);
    }


    /**
     * 列出与特定上游相关联的目标
     *
     * @param upstreamId
     * @return
     */
    public KongEntity<KongTarget> targetsByUpstreamId(String upstreamId) {
        return feginClient.targetsByUpstreamId(upstreamId);
    }

    public KongTarget addTarget(String upstreamId,KongTarget target){
        return  feginClient.addTarget(upstreamId,target);
    }


    public KongTarget deleteTarget(String upstreamId,String id){
        return  feginClient.deleteTarget(upstreamId,id);
    }

    /******************************消费者**************************/

    /**
     * 消费者列表
     *
     * @return
     */
    public KongEntity<KongConsumer> consumers() {
        return feginClient.consumers();
    }


    public KongConsumer consumerById(String id) {
        return feginClient.consumerById(id);
    }

    public KongConsumer addConsumer(KongConsumer consumer){
        return feginClient.addConsumer(consumer);
    }

    public void deleteConsumer(String id){
        feginClient.deleteConsumer(id);
    }

    public KongConsumer updateConsumer(String id,KongConsumer consumer){
        return feginClient.updateConsumer(id,consumer);
    }

    public KongEntity<KongPlugin> consumerPlugins(String id){
        return feginClient.consumerPlugins(id);
    }



    public KongEntity<Map> consumerCredentialByName(String id,String name){
        return feginClient.consumerCredentialByName(id,name);
    }


    public KongEntity<Map> addConsumerCredential(String id,String name,Map entity){
        return feginClient.addConsumerCredential(id,name,entity);
    }


    public void deleteConsumerCredential(String id,String name,String entityId){
        feginClient.deleteConsumerCredential(id,name,entityId);
    }








    /******************************证书**************************/

    public KongEntity<KongCertificate> certificates() {
        return feginClient.certificates();
    }

    public KongCertificate certificateById(String id){
        return feginClient.certificateById(id);
    }
    public KongCertificate addCertificate(KongCertificate certificate){
        return  feginClient.addCertificate(certificate);
    }
    public void deleteCertificate(String id){
        feginClient.deleteCertificate(id);
    }
    public KongCertificate updateCertificate(String id,KongCertificate certificate){
        return feginClient.updateCertificate(id,certificate);
    }


    /******************************CA证书**************************/

    public KongEntity<KongCaCertificate> caCertificates() {
        return feginClient.caCertificates();
    }


    /******************************SNI**************************/


    public KongEntity<KongSni> snis() {
        return feginClient.snis();
    }


    public KongSni sniById(String id){
        return feginClient.sniById(id);
    }

    public KongSni addSni(KongSni kongSni){
        return feginClient.addSni(kongSni);
    }

    public KongSni updateSni(String id,KongSni sni){
        return feginClient.updateSni(id,sni);
    }

    public void deleteSni(String id){
        feginClient.deleteSni(id);
    }


}
